package ClientSide;

import java.awt.Color;
import java.rmi.RemoteException;

import javax.swing.JTextArea;

public class Chat extends JTextArea{
	Perform_visualizza azione_visualizza;
	Perform_nascondi azione_nascondi;
	
	public Chat(){
	super();
	azione_nascondi=new Perform_nascondi();
	azione_nascondi.start();
	azione_visualizza=new Perform_visualizza();
	azione_visualizza.start();
	setDoubleBuffered(true);
    setCaretColor(new Color(255,255,255,160));
    setRows(0);
    setBackground(new Color(60,20,20,60));
    setForeground(new Color(255,255,255,170));
	addKeyListener(new java.awt.event.KeyAdapter () {
        public void keyPressed(java.awt.event.KeyEvent evt) {
            if(evt.getKeyCode() == evt.VK_ENTER)
            	{
            	 try {
					Client.instance.myServerObject.sendMessage(Client.instance.player.nome_player, getText());
				} catch (RemoteException e) {setText(e.getMessage());}
				setText("");
				setCaretPosition(0);
            	}
            
        }
             });
    addMouseListener(new java.awt.event.MouseAdapter(){
    	public void mouseEntered (java.awt.event.MouseEvent evt){
    		synchronized (azione_visualizza) {
    			azione_visualizza.notify();
			}
    	}
    	public void mouseExited (java.awt.event.MouseEvent evt){
    		synchronized (azione_nascondi) {
				azione_nascondi.notify();
			}
    	}
    });
	}

	
	private class Perform_visualizza extends Thread{
		public boolean abbandona=false;
		public void run(){
		  while(!isInterrupted())
		   {
			synchronized (this) {try{wait();}catch(InterruptedException ex){return;};}
			 int alpha=60;
			 abbandona=false;
			 azione_nascondi.abbandona=true;
			  while (alpha<255 || abbandona)
				{
				  System.out.println("alpha= "+alpha);
				  if(alpha<=255)
				  	{setBackground(new Color(60,20,20,alpha));}
				  alpha=alpha+15;
				  try{Thread.sleep(60);}catch (InterruptedException ex){}
				}
			  
		   }
		}
	}
	
	private class Perform_nascondi extends Thread{
		public boolean abbandona=false;
		public void run(){
		  while(!isInterrupted())
		   {
			synchronized (this) {try{wait();}catch(InterruptedException ex){return;};}
			  abbandona=false;
			  azione_visualizza.abbandona=true;
			 int alpha=255;
			  while (alpha>60 || abbandona)
				{
				  if(alpha<=255)
				     {setBackground(new Color(60,20,20,alpha));}
				  alpha=alpha-15;
				  try{Thread.sleep(60);}catch (InterruptedException ex){}
				}
		   }
		}
	}
	
	
}
